﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using log4net;
using System.Collections;
using System.Web;

[assembly: log4net.Config.XmlConfigurator(Watch = false)]
namespace SF.Base.Core
{
	public class Logger
	{
		private static string SPACE = " ";

		private static string DEL = "	";
		private static string TAB = "	:	";
		private static string TABS = "				";

		private static ILog _detaillogger;
		/// <summary>
		/// 日志记录器
		/// </summary>
		private static ILog DetailLogger
		{
			get
			{
				return _detaillogger;
			}
		}

		private static ILog _debuglogger;
		/// <summary>
		/// 日志记录器
		/// </summary>
		private static ILog DebugLogger
		{
			get
			{
				return _debuglogger;
			}
		}

		private static ILog _exceptionLogger;
		/// <summary>
		/// 异常日志记录器
		/// </summary>
		private static ILog ExceptionLogger
		{
			get
			{
				return _exceptionLogger;
			}
		}

		static Logger()
		{
			try
			{
				log4net.Config.XmlConfigurator.Configure();
				_detaillogger = log4net.LogManager.GetLogger("DetailLogger");
				_debuglogger = log4net.LogManager.GetLogger("DebugLogger");
				_exceptionLogger = log4net.LogManager.GetLogger("ExceptionLogger");
			}
			catch
			{

			}
		}

		public static void Start()
		{
			Detail("-------------start-------------");
		}

		public static void Start(string flag)
		{
			Detail("-------------start-------------");
			Detail(TABS,flag);
		}

		public static void Append(int i)
		{
			Detail(i);
		}

		public static void Append(long i)
		{
			Detail(i);
		}

		public static void Append(Boolean b)
		{
			Detail(b);
		}

		public static void Append(string key, Boolean b)
		{
			Detail(key, b.ToString());
		}

		public static void Append(string key, int count)
		{
			Detail(key, count.ToString());
		}
		public static void Append(long key, string count)
		{
			Detail(key.ToString(), count.ToString());
		}

		public static void Append(params string[] msgs)
		{
			StringBuilder sb = new StringBuilder();

			for (int i = 0; i < msgs.Length; i++)
			{
				sb.Append(msgs[i]);
				if (i != msgs.Length - 1)
				{
					sb.Append(DEL);
				}
			}
			Detail(sb.ToString());
		}

		public static void Append<T>(List<T> lists)
		{
			string key = lists.GetType().ToString();
			if (lists == null)
			{
				Detail(key, "0");
			}
			else
			{
				Detail(key, lists.Count.ToString());
			}
		}

		public static void Append<T>(string key,List<T> lists)
		{
			if (lists == null)
			{
				Detail(key, "0");
			}
			else
			{
				Detail(key, lists.Count.ToString());
			}
		}

		public static void End(string end)
		{
			Detail(TABS, end);
			End();
		}

		public static void End()
		{
			Detail("-------------end-------------");
		}

		public static void Detail<T>(string key,string key1, List<T> lists)
		{
			string type = lists.GetType().ToString();
			if (lists == null)
			{
				Detail(key,key1,type, 0);
			}
			else
			{
				Detail(key, key1,type, lists.Count);
			}
		}

		public static void Detail(string key, string key1, int i)
		{
			Detail(key, key1, i.ToString());
		}

		public static void Detail(string key, string key1, long i)
		{
			Detail(key, key1, i.ToString());
		}

		public static void Detail(string key, string key1,string key2,int i)
		{
			Detail(key, key1, key2, i.ToString());
		}

		public static void Detail<T>(string key, List<T> lists)
		{
			if (lists == null)
			{
				Detail(key, 0);
			}
			else
			{
				Detail(key, lists.Count);
			}
		}

		public static void Detail<T>(List<T> lists)
		{
			string key = lists.GetType().ToString();
			if (lists == null)
			{
				Detail(key, 0);
			}
			else
			{
				Detail(key,lists.Count);
			}
		}

		public static void Detail(int i)
		{
			Detail(i.ToString());
		}

		public static void Detail(long i)
		{
			Detail(i.ToString());
		}

		public static void Detail(string key,long i)
		{
			Detail(i.ToString());
		}

		public static void Detail(Boolean b)
		{
			Detail(b.ToString());
		}

		public static void Detail(string key, Boolean b)
		{
			Detail(key, b.ToString());
		}

		public static void Detail(string key, int count)
		{
			Detail(key, count.ToString());
		}

        public static void Detail(int key, string value)
        {
            Detail(key.ToString(), value);
        }

		public static void Debug(params string[] msgs)
		{
			string loginId = GetUserName();

			try
			{
				StringBuilder sb = new StringBuilder();

				sb.Append(loginId).Append(SPACE);

				for (int i = 0; i < msgs.Length; i++)
				{
					sb.Append(msgs[i]);
					if (i != msgs.Length - 1)
					{
						sb.Append(DEL);
					}
				}
				DebugLogger.Info(sb.ToString());
			}
			catch
			{

			}
		}

		public static void Detail(params string[] msgs)
		{
			string loginId = GetUserName();

			try
			{
				StringBuilder sb = new StringBuilder();

				sb.Append(loginId).Append(SPACE);

				for (int i = 0; i < msgs.Length; i++)
				{
					sb.Append(msgs[i]);
					if (i != msgs.Length - 1)
					{
						sb.Append(DEL);
					}
				}
				DetailLogger.Info(sb.ToString());
			}
			catch
			{

			}
		}

		public static void DetailV(params string[] msgs)
		{
			string loginId = GetUserName();

			try
			{
				StringBuilder sb = new StringBuilder();

				sb.AppendLine(loginId);

				for (int i = 0; i < msgs.Length; i++)
				{
					sb.AppendLine(TABS + msgs[i]);
				}
				DetailLogger.Info(sb.ToString());
			}
			catch
			{

			}
		}

		public static void Exception(string title, Exception ex)
		{
			string loginId = GetUserName();
			try
			{
				DetailLogger.Error(loginId + SPACE + title);
				ExceptionLogger.Error(FormatExceptionData(ex));
			}
			catch
			{

			}
		}

		public static void Exception(Exception ex)
		{
			string loginId = GetUserName();
			try
			{
				DetailLogger.Error(loginId + SPACE + ex.Message);
				ExceptionLogger.Error(FormatExceptionData(ex));
			}
			catch
			{

			}
		}

		public static void Exception(Exception ex, params string[] msgs)
		{
			string loginId = GetUserName();
			try
			{
				StringBuilder sb = new StringBuilder();
				sb.Append(loginId).Append(SPACE);

				for (int i = 0; i < msgs.Length; i++)
				{
					sb.Append(msgs[i]).Append(DEL);
				}

				ExceptionLogger.Info(sb.ToString());

				DetailLogger.Error(sb.ToString());

				DetailLogger.Error(ex.Message);

				ExceptionLogger.Error(FormatExceptionData(ex));

			}
			catch
			{

			}
		}

		public static void ErrorV(params string[] msgs)
		{
			string loginId = GetUserName();

			try
			{
				StringBuilder sb = new StringBuilder();
				sb.AppendLine(loginId);
				for (int i = 0; i < msgs.Length; i++)
				{
					sb.AppendLine(TABS + msgs[i]);
				}

				DetailLogger.Error(sb.ToString());
				ExceptionLogger.Error(sb.ToString());
			}
			catch
			{

			}
		}

		public static void Error(params string[] msgs)
		{
			string loginId = GetUserName();

			try
			{
				StringBuilder sb = new StringBuilder();
				sb.Append(loginId + SPACE);
				for (int i = 0; i < msgs.Length; i++)
				{
					sb.Append(msgs[i]).Append(DEL);
				}

				DetailLogger.Error(sb.ToString());
				ExceptionLogger.Error(sb.ToString());
			}
			catch
			{

			}
		}

		/// <summary>
		/// 用户登录PTID
		/// </summary>
		/// <returns></returns>
		public static string GetUserName()
		{
			if (HttpContext.Current != null
				&& HttpContext.Current.User != null
				&& HttpContext.Current.User.Identity != null)
			{
				try
				{
					return HttpContext.Current.User.Identity.Name;
				}
				catch
				{
					return string.Empty;
				}
			}
			else
			{
				return string.Empty;
			}
		}

		/// <summary>
		/// 获取格式化的业务对象数据
		/// </summary>
		/// <param name="ex"></param>
		/// <returns></returns>
		public static string FormatExceptionData(Exception ex)
		{
			string loginId = GetUserName();

			StringBuilder sb = new StringBuilder();

			sb.AppendLine(GetErrorTitle(ex.Message));

			if (ex != null && ex.Data != null)
			{
				IEnumerator ir = ex.Data.Keys.GetEnumerator();
				while (ir.MoveNext())
				{
					string key = (string)ir.Current;
					sb.Append(TAB + key);
					sb.Append(" : ");
					sb.Append(ex.Data[key].ToString());
					sb.Append(Environment.NewLine);
				}
			}
			sb.Append(TAB + ex.ToString()).Append(Environment.NewLine);
			return sb.ToString();
		}


		public static string GetErrorTitle(string message)
		{
			string loginId = GetUserName();
			StringBuilder sb = new StringBuilder();
			sb.AppendLine(loginId + SPACE + message).AppendLine("");
			sb.AppendLine(TAB + "1. 目标机器：" + Environment.MachineName);
			sb.AppendLine(TAB + "2. 请求地址：" + UtilHelper.GetRequestUrl());
			sb.AppendLine(TAB + "3. 客户端IP：" + UtilHelper.GetRealIP());

			return sb.ToString();
		}
	}
}
